#include <stdio.h>

#include "enum.h"
#include "zlog/zlog.h"

#include "elog.h"
#include "ipc_evt_api.h"
#include "ipc_evt_def.h"

#define TAG "call_manager"
static const int appSubscribeIpcList[] = {
#if defined(PROJECT_NUMBER_H53Y)
    CALL_MUTE_STATUS_NOTIFY,
// #elif (defined(PROJECT_NUMBER_H53A)||defined(PROJECT_NUMBER_H53B)||defined(PROJECT_NUMBER_H37A)||defined(PROJECT_NUMBER_H37A_VAVE)||defined(PROJECT_NUMBER_H37B))
#else
    TCU_MCU_ECALL_CTL_Req,
    // TESTER_CALL_TEST_ECALL_TRIGGER_REQ,
    SOMEIP_ACU_CRASH_NOTIFY,
    CALL_MODEM_ECALL_DIAL_RSP,
    CALL_MODEM_ECALL_PHONE_STATE_RSP,
    CALL_MODEM_AUDIO_PLAY_RSP,
    CALL_MODEM_AUDIO_PLAY_STATE_RSP,
    CALL_MODEM_SEND_SMS_RSP,
    MCU_VIN_Rsp,
    MODEM_ECALL_LOCATION_RSP,
    CALL_MODEM_AUDIO_STATE_RSP,
    CALL_SET_ECALL_PHONE_NUMBER_REQ,
#endif
    Modem_System_Goto_Sleep_Req,
    Modem_System_Wakeup_Rsp,
    MCU_AvailableStatus_Rsp,
#if defined(PROJECT_NUMBER_H53B)
    // PNC电源管理需求-20240430
    MODEM_WAKEUP_SOURCE_SETUP_RSP,
// #elif (defined(PROJECT_NUMBER_H53A)||defined(PROJECT_NUMBER_H53Y)||defined(PROJECT_NUMBER_H37A)||defined(PROJECT_NUMBER_H37A_VAVE)||defined(PROJECT_NUMBER_H37B))
#else
#endif
// 增加IPC心跳机制-20240527_1400
#if defined(DIAG_HEARTBEAT_FLAG)
    DIAGNOSTIC_HEARTBEAT_REQ,
#endif
};


int IPCEventCallback(int iType , void * pData , int iLength)
{
    // LOGI(TAG, "[RECV] iType(%08X), iLength(%d)\n", iType, iLength);
    // LOG_PRINT_HEX("pData", pData, iLength);

    switch (iType)
    {

        case Modem_System_Wakeup_Rsp:
        {
            // LOGI(TAG, "WKAEUP\n");
            printf("wakeup\n");
            break;
        }

        case MCU_AvailableStatus_Rsp:
        {
            //
            break;
        }

            //休眠通知
    case Modem_System_Goto_Sleep_Req:
    {
        // LOGI(TAG, "goto sleep\n");
        // LOGI(TAG, "goto sleep\n");
        printf("goto sleep\n");
    }
    break;


        default:
        {
            LOGW(TAG, "IPC type unknown!!\n");
            break;
        }
    }
    return 0;
}

int IPCEventSend(int iType, void *pData, int iLength)
{
    // LOGI(TAG, "[SEND] iType(%08X), iLength(%d)\n", iType, iLength);
    // LOG_PRINT_HEX("pData", pData, iLength);
    
    int iRet = publish_ipc_evt(iType, pData, iLength);
    return iRet;
}

int main(void){
    printf("hello 123\n");
    int rc = 0;
    zlog_category_t *c = NULL;
    rc = zlog_init("./cfg/zlog.conf");
    if(rc){
        printf("zlog_init failed,cfg dir ./cfg/zlog.conf\n");
        return ZLOG_ERROR_init;
    }
    printf("zlog_init success\n");

    c = zlog_get_category("app");
    // if(NULL == c){
    if(!c){
        printf("zlog_get_category failed\n");
        return ZLOG_ERROR_get_category;
    }
    printf("zlog_get_category success\n");

    zlog_debug(c, "hello, zlog - debug");
    zlog_info(c, "hello, zlog - info");
    zlog_notice(c, "hello, zlog - notice");
    zlog_warn(c, "hello, zlog - warn");
    zlog_error(c, "hello, zlog - error");
    zlog_fatal(c, "hello, zlog - fatal");

    char hex_buf[32];
    for (int i = 0; i < sizeof(hex_buf); i++) {
        hex_buf[i] = i;
    }
    hzlog_debug(c, hex_buf, sizeof(hex_buf));

    zlog_fini();

    elog_init();

    int iRet = initialize_ipc_evt(IPCEventCallback, "IPC_APP_call_manager");
    if (-1 == iRet)
    {
        LOGE(TAG, "initialize_ipc_evt Failed!!\n");
        return false;
    }

    for (unsigned int i = 0; i < (sizeof(appSubscribeIpcList) / sizeof(appSubscribeIpcList[0])); i++)
    {
        iRet = subscribe_ipc_evt(appSubscribeIpcList[i]);
        if (-1 == iRet)
        {
            LOGE(TAG, "subscribe_ipc_evt Failed!!\n");
            (void)finalize_ipc_evt();
            return false;
        }
    }

    LOGE(TAG, "Initialize start!!\n");
    run_ipc_evt_loop();
    LOGE(TAG, "Initialize Failed!!\n");

    if (0 != finalize_ipc_evt())
    {
        return false;
    }

    LOGE(TAG, "finalize_ipc_evt Failed!!\n");
    //elog_fini();
    Quit_ShareMemory();

    return 0;
}